<?php
defined('IN_IA') or exit('Access Denied');
class Asterye_waimaixia_plugin_oneModuleSite extends WeModuleSite {
	public function doWebAd(){
		global $_W, $_GPC;
		
		$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
		if ($operation == 'display') {
			if (!empty($_GPC['displayorder'])) {
				foreach ($_GPC['displayorder'] as $id => $displayorder) {
					pdo_update($this->modulename . '_ad', array('displayorder' => $displayorder), array('id' => $id, 'uniacid' => $_W['uniacid']));
				}
				message('广告排序更新成功！', $this->createWebUrl('ad', array('op' => 'display')), 'success');
			}
			
			$list = pdo_fetchall("SELECT * FROM ".tablename($this->modulename . '_ad')." WHERE cateid >= 1 AND cateid <= 10 AND uniacid = {$_W['uniacid']} ORDER BY displayorder desc");
		} elseif ($operation == 'post') {
			$id = intval($_GPC['id']);
			$data = $_GPC['data'];
			$data['uniacid'] = $_W['uniacid'];
			if (checksubmit('submit')) {
				$settings = $this->module['config'];
				$appid_type = $data["appid_type"];//1为外卖2为商超
				if($appid_type == 1 || $appid_type == 2){
					if($settings['meituan_api_type'] == 1){
						if($appid_type == 1){
							if(!intval($settings["meituan_waimai_h5_url"])){$settings['meituan_waimai_h5_url'] = 2;}
							$params = array('actId' => $settings['meituan_waimai_h5_url'],'key' => $settings['meituanappkey'],'sid' => $settings['meituanappkey_sid'],'linkType' => 4,'sign' => 1);
							$meituansign = genSign($params,$settings['meituanapikey']);
							$res = file_get_contents('https://runion.meituan.com/generateLink?actId='.$settings['meituan_waimai_h5_url'].'&key='.$settings['meituanappkey'].'&sid='.$settings['meituanappkey_sid'].'&linkType=4&sign='.$meituansign);
							$res = json_decode($res,true);
							if($res['status'] == 0){
								$data['appid'] = 'wxde8ac0a21135c07d';
							    $data['path'] = $res['data'];
							}
						}elseif($appid_type == 2){
							if(!intval($settings["meituan_sc_h5_url"])){$settings['meituan_sc_h5_url'] = 4;}
							$params = array('actId' => $settings['meituan_sc_h5_url'],'key' => $settings['meituanappkey'],'sid' => $settings['meituanappkey_sid'],'linkType' => 4,'sign' => 1);
							$meituansign = genSign($params,$settings['meituanapikey']);
							$res = file_get_contents('https://runion.meituan.com/generateLink?actId='.$settings['meituan_sc_h5_url'].'&key='.$settings['meituanappkey'].'&sid='.$settings['meituanappkey_sid'].'&linkType=4&sign='.$meituansign);
							$res = json_decode($res,true);
							if($res['status'] == 0){
								$data['appid'] = 'wxde8ac0a21135c07d';
							    $data['path'] = $res['data'];
							}
						}
					}else{
						$postdata = http_build_query(array('apikey' => $settings['ddxapikey'],'sid' => '999999','generate_we_app' => true,'qrcode' => true));
						$options = array(
								'http' => array(
								'method' => 'POST',
								'header' => 'Content-type:application/x-www-form-urlencoded',
								'content' => $postdata,
								'timeout' => 1 * 60 // 超时时间（单位:s）
							  )
							);
						$cxContext = stream_context_create($options);
						if($appid_type == 1){
							//外卖
							$wm = file_get_contents("http://api.tbk.dingdanxia.com/waimai/meituan_privilege", false, $cxContext);
							$wm = json_decode($wm,true);
							if($wm['code'] == 200){
								$data['appid'] = 'wxde8ac0a21135c07d';
							    $data['path'] = $wm['data']['we_app_info']['page_path'];
							}else{
								$data['appid'] = '';
							    $data['path'] = '';
							}
						}elseif($appid_type == 2){
							//闪购
							$wm = file_get_contents("http://api.tbk.dingdanxia.com/waimai/meituan_sg_privilege", false, $cxContext);
							$wm = json_decode($wm,true);
							if($wm['code'] == 200){
								$data['appid'] = 'wxde8ac0a21135c07d';
							    $data['path'] = $wm['data']['we_app_info']['page_path'];
							}else{
								$data['appid'] = '';
							    $data['path'] = '';
							}
						}
					}
					
				}elseif($appid_type == 3 || $appid_type == 4){
					//查询是否有pid空闲
					
					$pid = $settings['tbpid'];
					
					if($pid){
						$pid_array = explode("_",$pid);
						if($appid_type == 3){
							$elmwm_url = json_decode(file_get_contents('https://api.zhetaoke.com:10001/api/open_activitylink_get.ashx?appkey='.$settings['appkey'].'&sid='.$settings['sid'].'&adzone_id='.$pid_array[3].'&site_id='.$pid_array[2].'&promotion_scene_id='.$settings['elmwm_promotion_scene_id']),true);
							if($elmwm_url['tbk_sc_activity_info_get_response']['data']['short_click_url']){
								$data['appid'] = 'wxece3a9a4c82f58c9';
								$data['path'] = $elmwm_url['tbk_sc_activity_info_get_response']['data']['wx_miniprogram_path'];
							}else{
								$data['appid'] = '';
								$data['path'] = '';
							}
						}elseif($appid_type == 4){
							if(!$settings['elmsc_promotion_scene_id']){$settings['elmsc_promotion_scene_id'] = '1585018034441';}
							$elmsc_url = json_decode(file_get_contents('https://api.zhetaoke.com:10001/api/open_activitylink_get.ashx?appkey='.$settings['appkey'].'&sid='.$settings['sid'].'&adzone_id='.$pid_array[3].'&site_id='.$pid_array[2].'&promotion_scene_id='.$settings['elmsc_promotion_scene_id']),true);
							if($elmsc_url['tbk_sc_activity_info_get_response']['data']['short_click_url']){
								$data['appid'] = 'wxece3a9a4c82f58c9';
								$data['path'] = $elmsc_url['tbk_sc_activity_info_get_response']['data']['wx_miniprogram_path'];
							}else{
								$data['appid'] = '';
								$data['path'] = '';
							}
						}
					}
				
				}
				if (!empty($id)) {
					$res=pdo_update($this->modulename . '_ad', $data, array('id' => $id));
				} else {
					$res=pdo_insert($this->modulename . '_ad', $data);
				}
				if($res){
					message('操作成功！', $this->createWebUrl('ad', array('op' => 'display')), 'success');
				}else{
					message('操作失败！', $this->createWebUrl('ad', array('op' => 'display')), 'error');
				}
				
			}
			$adv = pdo_fetch("select * from ".tablename($this->modulename . '_ad')." where id = {$id} and uniacid= {$_W['uniacid']}");
		} elseif ($operation == 'delete') {
			$id = intval($_GPC['id']);
			$adv = pdo_fetch("SELECT id FROM ".tablename($this->modulename . '_ad')." WHERE id = {$id} AND uniacid = {$_W['uniacid']}");
			if (empty($adv)) {
				message('抱歉，广告不存在或是已经被删除！', $this->createWebUrl('ad', array('op' => 'display')), 'error');
			}
			pdo_delete($this->modulename.'_ad', array('id' => $id));
			message('广告删除成功！', $this->createWebUrl('ad', array('op' => 'display')), 'success');
		} elseif ($operation == 'show') {
			$id = intval($_GPC['id']);
			$adv = pdo_fetch("SELECT * FROM ".tablename($this->modulename . '_ad')." WHERE id = {$id} AND uniacid = {$_W['uniacid']}");
			if (empty($adv)) {
				message('抱歉，广告不存在或是已经被删除！', $this->createWebUrl('ad', array('op' => 'display')), 'error');
			}
			if($adv['is_show'] == 1){
				$res=pdo_update($this->modulename . '_ad', array('is_show' => 0), array('id' => $id));
			}else{
				$res=pdo_update($this->modulename . '_ad', array('is_show' => 1), array('id' => $id));
			}
			message('修改成功！', $this->createWebUrl('ad', array('op' => 'display')), 'success');
		} else {
			message('请求方式不存在');
		}
		include $this->template('web/adv');

	}
	public function doWebWaimaitt(){
		global $_W, $_GPC;
		$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
		if ($operation == 'display') {
			if (!empty($_GPC['displayorder'])) {
				foreach ($_GPC['displayorder'] as $id => $displayorder) {
					pdo_update($this->modulename . '_ad', array('displayorder' => $displayorder), array('id' => $id, 'uniacid' => $_W['uniacid']));
				}
				message('排序更新成功！', $this->createWebUrl('waimaitt', array('op' => 'display')), 'success');
			}
			
			$list = pdo_fetchall("SELECT * FROM ".tablename($this->modulename . '_ad')." WHERE cateid >= 21 AND cateid <= 29 AND uniacid = {$_W['uniacid']} ORDER BY displayorder desc");
		} elseif ($operation == 'post') {
			$id = intval($_GPC['id']);
			$data = $_GPC['data'];
			$data['uniacid'] = $_W['uniacid'];
			if (checksubmit('submit')) {
				$settings = $this->module['config'];
				$appid_type = $data["appid_type"];//1为外卖2为商超
				if($appid_type == 1 || $appid_type == 2){
					if($settings['meituan_api_type'] == 1){
						if($appid_type == 1){
							if(!intval($settings["meituan_waimai_h5_url"])){$settings['meituan_waimai_h5_url'] = 2;}
							$params = array('actId' => $settings['meituan_waimai_h5_url'],'key' => $settings['meituanappkey'],'sid' => $settings['meituanappkey_sid'],'linkType' => 4,'sign' => 1);
							$meituansign = genSign($params,$settings['meituanapikey']);
							$res = file_get_contents('https://runion.meituan.com/generateLink?actId='.$settings['meituan_waimai_h5_url'].'&key='.$settings['meituanappkey'].'&sid='.$settings['meituanappkey_sid'].'&linkType=4&sign='.$meituansign);
							$res = json_decode($res,true);
							if($res['status'] == 0){
								$data['appid'] = 'wxde8ac0a21135c07d';
							    $data['path'] = $res['data'];
							}
						}elseif($appid_type == 2){
							if(!intval($settings["meituan_sc_h5_url"])){$settings['meituan_sc_h5_url'] = 4;}
							$params = array('actId' => $settings['meituan_sc_h5_url'],'key' => $settings['meituanappkey'],'sid' => $settings['meituanappkey_sid'],'linkType' => 4,'sign' => 1);
							$meituansign = genSign($params,$settings['meituanapikey']);
							$res = file_get_contents('https://runion.meituan.com/generateLink?actId='.$settings['meituan_sc_h5_url'].'&key='.$settings['meituanappkey'].'&sid='.$settings['meituanappkey_sid'].'&linkType=4&sign='.$meituansign);
							$res = json_decode($res,true);
							if($res['status'] == 0){
								$data['appid'] = 'wxde8ac0a21135c07d';
							    $data['path'] = $res['data'];
							}
						}
					}else{
						$postdata = http_build_query(array('apikey' => $settings['ddxapikey'],'sid' => '999999','generate_we_app' => true,'qrcode' => true));
						$options = array(
								'http' => array(
								'method' => 'POST',
								'header' => 'Content-type:application/x-www-form-urlencoded',
								'content' => $postdata,
								'timeout' => 1 * 60 // 超时时间（单位:s）
							  )
							);
						$cxContext = stream_context_create($options);
						if($appid_type == 1){
							//外卖
							$wm = file_get_contents("http://api.tbk.dingdanxia.com/waimai/meituan_privilege", false, $cxContext);
							$wm = json_decode($wm,true);
							if($wm['code'] == 200){
								$data['appid'] = 'wxde8ac0a21135c07d';
							    $data['path'] = $wm['data']['we_app_info']['page_path'];
							}else{
								$data['appid'] = '';
							    $data['path'] = '';
							}
						}elseif($appid_type == 2){
							//闪购
							$wm = file_get_contents("http://api.tbk.dingdanxia.com/waimai/meituan_sg_privilege", false, $cxContext);
							$wm = json_decode($wm,true);
							if($wm['code'] == 200){
								$data['appid'] = 'wxde8ac0a21135c07d';
							    $data['path'] = $wm['data']['we_app_info']['page_path'];
							}else{
								$data['appid'] = '';
							    $data['path'] = '';
							}
						}
					}
					
				}elseif($appid_type == 3 || $appid_type == 4){
					//查询是否有pid空闲
					
					$pid = $settings['tbpid'];
					
					if($pid){
						$pid_array = explode("_",$pid);
						if($appid_type == 3){
							$elmwm_url = json_decode(file_get_contents('https://api.zhetaoke.com:10001/api/open_activitylink_get.ashx?appkey='.$settings['appkey'].'&sid='.$settings['sid'].'&adzone_id='.$pid_array[3].'&site_id='.$pid_array[2].'&promotion_scene_id='.$settings['elmwm_promotion_scene_id']),true);
							if($elmwm_url['tbk_sc_activity_info_get_response']['data']['short_click_url']){
								$data['appid'] = 'wxece3a9a4c82f58c9';
								$data['path'] = $elmwm_url['tbk_sc_activity_info_get_response']['data']['wx_miniprogram_path'];
							}else{
								$data['appid'] = '';
								$data['path'] = '';
							}
						}elseif($appid_type == 4){
							if(!$settings['elmsc_promotion_scene_id']){$settings['elmsc_promotion_scene_id'] = '1585018034441';}
							$elmsc_url = json_decode(file_get_contents('https://api.zhetaoke.com:10001/api/open_activitylink_get.ashx?appkey='.$settings['appkey'].'&sid='.$settings['sid'].'&adzone_id='.$pid_array[3].'&site_id='.$pid_array[2].'&promotion_scene_id='.$settings['elmsc_promotion_scene_id']),true);
							if($elmsc_url['tbk_sc_activity_info_get_response']['data']['short_click_url']){
								$data['appid'] = 'wxece3a9a4c82f58c9';
								$data['path'] = $elmsc_url['tbk_sc_activity_info_get_response']['data']['wx_miniprogram_path'];
							}else{
								$data['appid'] = '';
								$data['path'] = '';
							}
						}
					}
				
				}
				if (!empty($id)) {
					$res=pdo_update($this->modulename . '_ad', $data, array('id' => $id));
				} else {
					$res=pdo_insert($this->modulename . '_ad', $data);
				}
				if($res){
					message('操作成功！', $this->createWebUrl('waimaitt', array('op' => 'display')), 'success');
				}else{
					message('操作失败！', $this->createWebUrl('waimaitt', array('op' => 'display')), 'error');
				}
				
			}
			$adv = pdo_fetch("select * from ".tablename($this->modulename . '_ad')." where id = {$id} and uniacid= {$_W['uniacid']}");
		} elseif ($operation == 'delete') {
			$id = intval($_GPC['id']);
			$adv = pdo_fetch("SELECT id FROM ".tablename($this->modulename . '_ad')." WHERE id = {$id} AND uniacid = {$_W['uniacid']}");
			if (empty($adv)) {
				message('抱歉，内容不存在或是已经被删除！', $this->createWebUrl('waimaitt', array('op' => 'display')), 'error');
			}
			pdo_delete($this->modulename.'_ad', array('id' => $id));
			message('删除成功！', $this->createWebUrl('waimaitt', array('op' => 'display')), 'success');
		} elseif ($operation == 'show') {
			$id = intval($_GPC['id']);
			$adv = pdo_fetch("SELECT * FROM ".tablename($this->modulename . '_ad')." WHERE id = {$id} AND uniacid = {$_W['uniacid']}");
			if (empty($adv)) {
				message('抱歉，内容不存在或是已经被删除！', $this->createWebUrl('waimaitt', array('op' => 'display')), 'error');
			}
			if($adv['is_show'] == 1){
				$res=pdo_update($this->modulename . '_ad', array('is_show' => 0), array('id' => $id));
			}else{
				$res=pdo_update($this->modulename . '_ad', array('is_show' => 1), array('id' => $id));
			}
			message('修改成功！', $this->createWebUrl('waimaitt', array('op' => 'display')), 'success');
		} else {
			message('请求方式不存在');
		}
		include $this->template('web/waimaitt');

	}
	public function doWebWaimai(){
		global $_W, $_GPC;
		$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
		if ($operation == 'display') {
			if (!empty($_GPC['displayorder'])) {
				foreach ($_GPC['displayorder'] as $id => $displayorder) {
					pdo_update($this->modulename . '_ad', array('displayorder' => $displayorder), array('id' => $id, 'uniacid' => $_W['uniacid']));
				}
				message('排序更新成功！', $this->createWebUrl('waimai', array('op' => 'display')), 'success');
			}
			
			$list = pdo_fetchall("SELECT * FROM ".tablename($this->modulename . '_ad')." WHERE cateid >= 11 AND cateid <= 19 AND uniacid = {$_W['uniacid']} ORDER BY displayorder desc");
		} elseif ($operation == 'post') {
			$id = intval($_GPC['id']);
			$data = $_GPC['data'];
			$data['uniacid'] = $_W['uniacid'];
			if (checksubmit('submit')) {
				$settings = $this->module['config'];
				$appid_type = $data["appid_type"];//1为外卖2为商超
				if($appid_type == 1 || $appid_type == 2){
					if($settings['meituan_api_type'] == 1){
						if($appid_type == 1){
							if(!intval($settings["meituan_waimai_h5_url"])){$settings['meituan_waimai_h5_url'] = 2;}
							$params = array('actId' => $settings['meituan_waimai_h5_url'],'key' => $settings['meituanappkey'],'sid' => $settings['meituanappkey_sid'],'linkType' => 4,'sign' => 1);
							$meituansign = genSign($params,$settings['meituanapikey']);
							$res = file_get_contents('https://runion.meituan.com/generateLink?actId='.$settings['meituan_waimai_h5_url'].'&key='.$settings['meituanappkey'].'&sid='.$settings['meituanappkey_sid'].'&linkType=4&sign='.$meituansign);
							$res = json_decode($res,true);
							if($res['status'] == 0){
								$data['appid'] = 'wxde8ac0a21135c07d';
							    $data['path'] = $res['data'];
							}
						}elseif($appid_type == 2){
							if(!intval($settings["meituan_sc_h5_url"])){$settings['meituan_sc_h5_url'] = 4;}
							$params = array('actId' => $settings['meituan_sc_h5_url'],'key' => $settings['meituanappkey'],'sid' => $settings['meituanappkey_sid'],'linkType' => 4,'sign' => 1);
							$meituansign = genSign($params,$settings['meituanapikey']);
							$res = file_get_contents('https://runion.meituan.com/generateLink?actId='.$settings['meituan_sc_h5_url'].'&key='.$settings['meituanappkey'].'&sid='.$settings['meituanappkey_sid'].'&linkType=4&sign='.$meituansign);
							$res = json_decode($res,true);
							if($res['status'] == 0){
								$data['appid'] = 'wxde8ac0a21135c07d';
							    $data['path'] = $res['data'];
							}
						}
					}else{
						$postdata = http_build_query(array('apikey' => $settings['ddxapikey'],'sid' => '999999','generate_we_app' => true,'qrcode' => true));
						$options = array(
								'http' => array(
								'method' => 'POST',
								'header' => 'Content-type:application/x-www-form-urlencoded',
								'content' => $postdata,
								'timeout' => 1 * 60 // 超时时间（单位:s）
							  )
							);
						$cxContext = stream_context_create($options);
						if($appid_type == 1){
							//外卖
							$wm = file_get_contents("http://api.tbk.dingdanxia.com/waimai/meituan_privilege", false, $cxContext);
							$wm = json_decode($wm,true);
							if($wm['code'] == 200){
								$data['appid'] = 'wxde8ac0a21135c07d';
							    $data['path'] = $wm['data']['we_app_info']['page_path'];
							}else{
								$data['appid'] = '';
							    $data['path'] = '';
							}
						}elseif($appid_type == 2){
							//闪购
							$wm = file_get_contents("http://api.tbk.dingdanxia.com/waimai/meituan_sg_privilege", false, $cxContext);
							$wm = json_decode($wm,true);
							if($wm['code'] == 200){
								$data['appid'] = 'wxde8ac0a21135c07d';
							    $data['path'] = $wm['data']['we_app_info']['page_path'];
							}else{
								$data['appid'] = '';
							    $data['path'] = '';
							}
						}
					}
					
				}elseif($appid_type == 3 || $appid_type == 4){
					//查询是否有pid空闲
					
					$pid = $settings['tbpid'];
					
					if($pid){
						$pid_array = explode("_",$pid);
						if($appid_type == 3){
							$elmwm_url = json_decode(file_get_contents('https://api.zhetaoke.com:10001/api/open_activitylink_get.ashx?appkey='.$settings['appkey'].'&sid='.$settings['sid'].'&adzone_id='.$pid_array[3].'&site_id='.$pid_array[2].'&promotion_scene_id='.$settings['elmwm_promotion_scene_id']),true);
							if($elmwm_url['tbk_sc_activity_info_get_response']['data']['short_click_url']){
								$data['appid'] = 'wxece3a9a4c82f58c9';
								$data['path'] = $elmwm_url['tbk_sc_activity_info_get_response']['data']['wx_miniprogram_path'];
							}else{
								$data['appid'] = '';
								$data['path'] = '';
							}
						}elseif($appid_type == 4){
							if(!$settings['elmsc_promotion_scene_id']){$settings['elmsc_promotion_scene_id'] = '1585018034441';}
							$elmsc_url = json_decode(file_get_contents('https://api.zhetaoke.com:10001/api/open_activitylink_get.ashx?appkey='.$settings['appkey'].'&sid='.$settings['sid'].'&adzone_id='.$pid_array[3].'&site_id='.$pid_array[2].'&promotion_scene_id='.$settings['elmsc_promotion_scene_id']),true);
							if($elmsc_url['tbk_sc_activity_info_get_response']['data']['short_click_url']){
								$data['appid'] = 'wxece3a9a4c82f58c9';
								$data['path'] = $elmsc_url['tbk_sc_activity_info_get_response']['data']['wx_miniprogram_path'];
							}else{
								$data['appid'] = '';
								$data['path'] = '';
							}
						}
					}
				
				}
				if (!empty($id)) {
					$res=pdo_update($this->modulename . '_ad', $data, array('id' => $id));
				} else {
					$res=pdo_insert($this->modulename . '_ad', $data);
				}
				if($res){
					message('操作成功！', $this->createWebUrl('waimai', array('op' => 'display')), 'success');
				}else{
					message('操作失败！', $this->createWebUrl('waimai', array('op' => 'display')), 'error');
				}
				
			}
			$adv = pdo_fetch("select * from ".tablename($this->modulename . '_ad')." where id = {$id} and uniacid= {$_W['uniacid']}");
		} elseif ($operation == 'delete') {
			$id = intval($_GPC['id']);
			$adv = pdo_fetch("SELECT id FROM ".tablename($this->modulename . '_ad')." WHERE id = {$id} AND uniacid = {$_W['uniacid']}");
			if (empty($adv)) {
				message('抱歉，内容不存在或是已经被删除！', $this->createWebUrl('waimai', array('op' => 'display')), 'error');
			}
			pdo_delete($this->modulename.'_ad', array('id' => $id));
			message('删除成功！', $this->createWebUrl('waimai', array('op' => 'display')), 'success');
		} elseif ($operation == 'show') {
			$id = intval($_GPC['id']);
			$adv = pdo_fetch("SELECT * FROM ".tablename($this->modulename . '_ad')." WHERE id = {$id} AND uniacid = {$_W['uniacid']}");
			if (empty($adv)) {
				message('抱歉，内容不存在或是已经被删除！', $this->createWebUrl('waimai', array('op' => 'display')), 'error');
			}
			if($adv['is_show'] == 1){
				$res=pdo_update($this->modulename . '_ad', array('is_show' => 0), array('id' => $id));
			}else{
				$res=pdo_update($this->modulename . '_ad', array('is_show' => 1), array('id' => $id));
			}
			message('修改成功！', $this->createWebUrl('waimai', array('op' => 'display')), 'success');
		} else {
			message('请求方式不存在');
		}
		include $this->template('web/waimai');

	}
	public function doWebCate(){
		global $_W, $_GPC;
		$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
		if ($operation == 'display') {
			if (!empty($_GPC['displayorder'])) {
				foreach ($_GPC['displayorder'] as $id => $displayorder) {
					pdo_update($this->modulename . '_tequan_cate', array('displayorder' => $displayorder), array('id' => $id, 'uniacid' => $_W['uniacid']));
				}
				message('分类排序更新成功！', $this->createWebUrl('cate', array('op' => 'display')), 'success');
			}
			$list = pdo_fetchall("SELECT * FROM ".tablename($this->modulename . '_tequan_cate')." WHERE uniacid = {$_W['uniacid']} ORDER BY displayorder DESC");
		} elseif ($operation == 'post') {
			$id = intval($_GPC['id']);
			if (checksubmit('submit')) {
				$data = $_GPC['data'];
				$data['uniacid'] = $_W['uniacid'];
				if (!empty($id)) {
					pdo_update($this->modulename . '_tequan_cate', $data, array('id' => $id));
				} else {
					pdo_insert($this->modulename . '_tequan_cate', $data);
				}
				message('操作成功！', $this->createWebUrl('cate', array('op' => 'display')), 'success');
			}
			$bigcat = pdo_fetch("select * from ".tablename($this->modulename . '_tequan_cate')." where id = {$id} and uniacid= {$_W['uniacid']}");
		} elseif ($operation == 'delete') {
			$id = intval($_GPC['id']);
			$bigcat = pdo_fetch("SELECT id FROM ".tablename($this->modulename . '_tequan_cate')." WHERE id = {$id} AND uniacid = {$_W['uniacid']}");
			if (empty($bigcat)) {
				message('抱歉，分类不存在或是已经被删除！', $this->createWebUrl('cate', array('op' => 'display')), 'error');
			}
			pdo_delete($this->modulename . '_tequan_cate', array('id' => $id));
			message('分类删除成功！', $this->createWebUrl('cate', array('op' => 'display')), 'success');
		} elseif ($operation == 'show') {
			$id = intval($_GPC['id']);
			$adv = pdo_fetch("SELECT * FROM ".tablename($this->modulename . '_tequan_cate')." WHERE id = {$id} AND uniacid = {$_W['uniacid']}");
			if (empty($adv)) {
				message('抱歉，分类不存在或是已经被删除！', $this->createWebUrl('cate', array('op' => 'display')), 'error');
			}
			if($adv['enabled'] == 1){
				$res=pdo_update($this->modulename . '_tequan_cate', array('enabled' => 0), array('id' => $id));
			}else{
				$res=pdo_update($this->modulename . '_tequan_cate', array('enabled' => 1), array('id' => $id));
			}
			message('修改成功！', $this->createWebUrl('cate', array('op' => 'display')), 'success');
		}else {
			message('请求方式不存在');
		}
		include $this->template('web/cate');

	}
	public function doWebGoods_cate(){
		global $_W, $_GPC;
		$settings = $this->module['config'];
		$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
		if ($operation == 'display') {
			if (!empty($_GPC['displayorder'])) {
				foreach ($_GPC['displayorder'] as $id => $displayorder) {
					pdo_update($this->modulename . '_goods_cate', array('displayorder' => $displayorder), array('id' => $id, 'uniacid' => $_W['uniacid']));
				}
				message('分类排序更新成功！', $this->createWebUrl('goods_cate', array('op' => 'display')), 'success');
			}
			$pindex = max(1, intval($_GPC['page']));
			$psize = 20;
			$total = pdo_fetchcolumn("SELECT COUNT(*) FROM ".tablename($this->modulename . '_goods_cate')." WHERE uniacid = ".$_W['uniacid']);
			$limit = ' LIMIT ' . ($pindex - 1) * $psize . ',' . $psize;
			$list = pdo_fetchall("SELECT * FROM ".tablename($this->modulename . '_goods_cate')." WHERE  uniacid = ".$_W['uniacid']." ORDER BY pintai DESC,displayorder DESC ".$limit);
			$pager = pagination($total, $pindex, $psize);
		} elseif ($operation == 'post') {
			$id = intval($_GPC['id']);
			if (checksubmit('submit')) {
				$data = $_GPC['data'];
				$data['uniacid'] = $_W['uniacid'];
				if (!empty($id)) {
					pdo_update($this->modulename . '_goods_cate', $data, array('id' => $id));
				} else {
					pdo_insert($this->modulename . '_goods_cate', $data);
				}
				message('操作成功！', $this->createWebUrl('goods_cate', array('op' => 'display')), 'success');
			}
			$bigcat = pdo_fetch("select * from ".tablename($this->modulename . '_goods_cate')." where id = {$id} and uniacid= {$_W['uniacid']}");
		} elseif ($operation == 'post_jd') {
			
			$jd = json_decode(file_get_contents('http://api-gw.haojingke.com/index.php/v1/api/jd/getcategory?parentId=0&grade=0&apikey='.$settings['apikey']),true);
			foreach ($jd['data'] as $key => $val) {
				$is_cz = pdo_get($this->modulename . "_goods_cate", array("cateid" => $val['id'],'uniacid' => $_W['uniacid']));
				if($is_cz){
					pdo_update($this->modulename . '_goods_cate', array("catename" => $val['name']), array('cateid' => $val['id'],'uniacid' => $_W['uniacid']));
				}else{
					pdo_insert($this->modulename . '_goods_cate', array("pintai" => 'jd',"catename" => $val['name'],'cateid' => $val['id'],'is_show' => 1,'uniacid' => $_W['uniacid']));
				}
			}
			message('同步分类成功！', $this->createWebUrl('goods_cate', array('op' => 'display')), 'success');

		} elseif ($operation == 'post_pdd') {
			
			$jd = json_decode(file_get_contents('http://api-gw.haojingke.com/index.php/v1/api/pdd/cats?parent_cat_id=0&apikey='.$settings['apikey']),true);
			foreach ($jd['data']['goods_cats_list'] as $key => $val) {
				$is_cz = pdo_get($this->modulename . "_goods_cate", array("cateid" => $val['cat_id'],'uniacid' => $_W['uniacid']));
				if($is_cz){
					pdo_update($this->modulename . '_goods_cate', array("catename" => $val['cat_name']), array('cateid' => $val['cat_id'],'uniacid' => $_W['uniacid']));
				}else{
					pdo_insert($this->modulename . '_goods_cate', array("pintai" => 'pdd',"catename" => $val['cat_name'],'cateid' => $val['cat_id'],'is_show' => 1,'uniacid' => $_W['uniacid']));
				}
			}
			message('同步分类成功！', $this->createWebUrl('goods_cate', array('op' => 'display')), 'success');

		} elseif ($operation == 'post_tb') {
			
			$jd = array('女装','母婴','美妆','居家日用','鞋品','美食','文娱车品','数码家电','男装','内衣','箱包','配饰','户外运动','家装家纺');
			$i = 1;
			foreach ($jd as $key => $val) {
				$is_cz = pdo_get($this->modulename . "_goods_cate", array("cateid" => $i,'uniacid' => $_W['uniacid']));
				if($is_cz){
					pdo_update($this->modulename . '_goods_cate', array("catename" => $val), array('cateid' => $i,'uniacid' => $_W['uniacid']));
				}else{
					pdo_insert($this->modulename . '_goods_cate', array("pintai" => 'tb',"catename" => $val,'cateid' => $i,'is_show' => 1,'uniacid' => $_W['uniacid']));
				}
				$i++;
			}
			message('同步分类成功！', $this->createWebUrl('goods_cate', array('op' => 'display')), 'success');

		} elseif ($operation == 'delete') {
			$id = intval($_GPC['id']);
			$bigcat = pdo_fetch("SELECT id FROM ".tablename($this->modulename . '_goods_cate')." WHERE id = {$id} AND uniacid = {$_W['uniacid']}");
			if (empty($bigcat)) {
				message('抱歉，分类不存在或是已经被删除！', $this->createWebUrl('goods_cate', array('op' => 'display')), 'error');
			}
			pdo_delete($this->modulename . '_goods_cate', array('id' => $id));
			message('分类删除成功！', $this->createWebUrl('goods_cate', array('op' => 'display')), 'success');
		} elseif ($operation == 'show') {
			$id = intval($_GPC['id']);
			$adv = pdo_fetch("SELECT * FROM ".tablename($this->modulename . '_goods_cate')." WHERE id = {$id} AND uniacid = {$_W['uniacid']}");
			if (empty($adv)) {
				message('抱歉，分类不存在或是已经被删除！', $this->createWebUrl('goods_cate', array('op' => 'display')), 'error');
			}
			if($adv['enabled'] == 1){
				$res=pdo_update($this->modulename . '_goods_cate', array('enabled' => 0), array('id' => $id));
			}else{
				$res=pdo_update($this->modulename . '_goods_cate', array('enabled' => 1), array('id' => $id));
			}
			message('修改成功！', $this->createWebUrl('goods_cate', array('op' => 'display')), 'success');
		}else {
			message('请求方式不存在');
		}
		include $this->template('web/goods_cate');

	}
	public function doWebGoods(){
		global $_W, $_GPC;
		$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
		if ($operation == 'display') {
			if (!empty($_GPC['displayorder'])) {
				foreach ($_GPC['displayorder'] as $id => $displayorder) {
					pdo_update($this->modulename . '_tequan_item', array('displayorder' => $displayorder), array('id' => $id, 'uniacid' => $_W['uniacid']));
				}
				message('商品排序更新成功！', $this->createWebUrl('goods', array('op' => 'display')), 'success');
			}
			$pindex = max(1, intval($_GPC['page']));
			$psize = 20;
			$total = pdo_fetchcolumn("SELECT COUNT(*) FROM ".tablename($this->modulename . '_tequan_item')." WHERE uniacid = ".$_W['uniacid']);
			$limit = ' LIMIT ' . ($pindex - 1) * $psize . ',' . $psize;
			$list = pdo_fetchall("SELECT * FROM ".tablename($this->modulename . '_tequan_item')." WHERE  uniacid = ".$_W['uniacid']." ORDER BY displayorder DESC ".$limit);
			$pager = pagination($total, $pindex, $psize);
		} elseif ($operation == 'post') {
			$id = intval($_GPC['id']);
			$data = $_GPC['data'];
			$data['uniacid'] = $_W['uniacid'];
			if (checksubmit('submit')) {
				if (!empty($id)) {
					$res=pdo_update($this->modulename . '_tequan_item', $data, array('id' => $id));
				} else {
					$res=pdo_insert($this->modulename . '_tequan_item', $data);
				}
				if($res){
					message('操作成功！', $this->createWebUrl('goods', array('op' => 'display')), 'success');
				}else{
					message('操作失败！', $this->createWebUrl('goods', array('op' => 'display')), 'error');
				}
				
			}
			$adv = pdo_fetch("select * from ".tablename($this->modulename . '_tequan_item')." where id = {$id} and uniacid= {$_W['uniacid']}");
			$cate = pdo_fetchall("SELECT * FROM ".tablename($this->modulename . '_tequan_cate')." WHERE uniacid = {$_W['uniacid']} and enabled = 1 ORDER BY displayorder desc");
		} elseif ($operation == 'delete') {
			$id = intval($_GPC['id']);
			$adv = pdo_fetch("SELECT id FROM ".tablename($this->modulename . '_tequan_item')." WHERE id = {$id} AND uniacid = {$_W['uniacid']}");
			if (empty($adv)) {
				message('抱歉，商品不存在或是已经被删除！', $this->createWebUrl('goods', array('op' => 'display')), 'error');
			}
			pdo_delete($this->modulename.'_tequan_item', array('id' => $id));
			message('商品删除成功！', $this->createWebUrl('goods', array('op' => 'display')), 'success');
		} elseif ($operation == 'show') {
			$id = intval($_GPC['id']);
			$adv = pdo_fetch("SELECT * FROM ".tablename($this->modulename . '_tequan_item')." WHERE id = {$id} AND uniacid = {$_W['uniacid']}");
			if (empty($adv)) {
				message('抱歉，商品不存在或是已经被删除！', $this->createWebUrl('goods', array('op' => 'display')), 'error');
			}
			if($adv['enabled'] == 1){
				$res=pdo_update($this->modulename . '_tequan_item', array('enabled' => 0), array('id' => $id));
			}else{
				$res=pdo_update($this->modulename . '_tequan_item', array('enabled' => 1), array('id' => $id));
			}
			message('修改成功！', $this->createWebUrl('goods', array('op' => 'display')), 'success');
		} else {
			message('请求方式不存在');
		}
		include $this->template('web/goods');

	}
	public function doWebGoods_feeds(){
		global $_W, $_GPC;
		$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
		if ($operation == 'display') {
			if (!empty($_GPC['displayorder'])) {
				foreach ($_GPC['displayorder'] as $id => $displayorder) {
					pdo_update($this->modulename . '_goodsfeeds', array('displayorder' => $displayorder), array('item_id' => $id, 'uniacid' => $_W['uniacid']));
				}
				message('商品排序更新成功！', $this->createWebUrl('goods_feeds', array('op' => 'display')), 'success');
			}
			$pindex = max(1, intval($_GPC['page']));
			$psize = 20;
			$total = pdo_fetchcolumn("SELECT COUNT(*) FROM ".tablename($this->modulename . '_goodsfeeds')." WHERE uniacid = ".$_W['uniacid']);
			$limit = ' LIMIT ' . ($pindex - 1) * $psize . ',' . $psize;
			$list = pdo_fetchall("SELECT * FROM ".tablename($this->modulename . '_goodsfeeds')." WHERE  uniacid = ".$_W['uniacid']." ORDER BY displayorder DESC ".$limit);
			$pager = pagination($total, $pindex, $psize);
		} elseif ($operation == 'post') {
			$id = intval($_GPC['id']);
			$data = $_GPC['data'];
			$data['photos'] = json_encode($data['photos']);
			$data['uniacid'] = $_W['uniacid'];
			if (checksubmit('submit')) {
				if (!empty($id)) {
					$res=pdo_update($this->modulename . '_goodsfeeds', $data, array('item_id' => $id));
				} else {
					$data['create_time'] = time();
					$res=pdo_insert($this->modulename . '_goodsfeeds', $data);
				}
				if($res){
					message('操作成功！', $this->createWebUrl('goods_feeds', array('op' => 'display')), 'success');
				}else{
					message('操作失败！', $this->createWebUrl('goods_feeds', array('op' => 'display')), 'error');
				}
				
			}
			$adv = pdo_fetch("select * from ".tablename($this->modulename . '_goodsfeeds')." where item_id = {$id} and uniacid= {$_W['uniacid']}");
		} elseif ($operation == 'delete') {
			$id = intval($_GPC['id']);
			$adv = pdo_fetch("SELECT item_id FROM ".tablename($this->modulename . '_goodsfeeds')." WHERE item_id = {$id} AND uniacid = {$_W['uniacid']}");
			if (empty($adv)) {
				message('抱歉，商品不存在或是已经被删除！', $this->createWebUrl('goods_feeds', array('op' => 'display')), 'error');
			}
			pdo_delete($this->modulename.'_goodsfeeds', array('item_id' => $id));
			message('商品删除成功！', $this->createWebUrl('goods_feeds', array('op' => 'display')), 'success');
		} elseif ($operation == 'show') {
			$id = intval($_GPC['id']);
			$adv = pdo_fetch("SELECT * FROM ".tablename($this->modulename . '_goodsfeeds')." WHERE item_id = {$id} AND uniacid = {$_W['uniacid']}");
			if (empty($adv)) {
				message('抱歉，商品不存在或是已经被删除！', $this->createWebUrl('goods_feeds', array('op' => 'display')), 'error');
			}
			if($adv['is_show'] == 1){
				$res=pdo_update($this->modulename . '_goodsfeeds', array('is_show' => 0), array('item_id' => $id));
			}else{
				$res=pdo_update($this->modulename . '_goodsfeeds', array('is_show' => 1), array('item_id' => $id));
			}
			message('修改成功！', $this->createWebUrl('goods_feeds', array('op' => 'display')), 'success');
		} else {
			message('请求方式不存在');
		}
		include $this->template('web/feeds');

	}
	

}

function genSign($params,$secret)
{
	unset($params["sign"]);
	ksort($params);
	$str = $secret; // $secret为分配的密钥
	foreach($params as $key => $value) {
		$str .= $key . $value;
	}
	$str .= $secret;
	$sign = md5($str);
	return $sign;
}
